package com.wondream.plumelog.example.logback.interceptor;

import com.wondream.plumelog.example.logback.utils.PlumeLogUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 实现 HandlerInterceptor 并重写需要的方法
 *
 * @author Demo_Null
 * @date 2020/8/27
 * @description 自定义拦截器
 */
@Component
@Slf4j
public class TraceIdInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler) throws Exception {
        System.out.println("在目标方法执行之前执行");
        PlumeLogUtils.setTraceIdInRequest(request);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, 
                           Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("在目标方法执行之后视图对象返回之前执行");

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, 
                                Object handler, Exception ex) throws Exception {
        System.out.println("在流程都执行完毕后执行");
    }
}
